package com.koko.controller;

import com.github.pagehelper.Page;
import com.koko.game.dto.CategoryCardDTO;
import com.koko.game.dto.CategoryDTO;
import com.koko.game.dto.CategoryPageQueryDTO;
import com.koko.game.dto.GameCategoriesDTO;
import com.koko.game.entity.Category;
import com.koko.game.vo.CategoryVO;
import com.koko.result.PageResult;
import com.koko.result.Result;
import com.koko.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/game")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 游戏分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/admin/category/page")
    @Cacheable(value = "categoryCache", key = "'categoryPage'")
    public PageResult<Category> adminManagementCategoryList(CategoryPageQueryDTO categoryPageQueryDTO) {
        Page<Category> page = categoryService.pageQuery(categoryPageQueryDTO);
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    /**
     * 新增游戏分类
     * @param categoryDTO
     * @return
     */
    @PostMapping("/admin/category")
    @CacheEvict(value = "categoryCache", key = "'categoryPage'")
    public Result<String> addCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /**
     * 批量删除游戏分类
     * @param ids
     * @return
     */
    @DeleteMapping("/admin/category")
    @CacheEvict(value = "categoryCache", allEntries = true)
    public Result<String> deleteCategory(@RequestBody List<Long> ids) {
        categoryService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改分类信息
     * @param categoryDTO
     * @return
     */
    @PutMapping("/admin/category")
    @CacheEvict(value = "categoryCache", key = "'categoryDetail' + '_' + #categoryDTO.id")
    public Result<String> updateCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id查询游戏分类详细信息
     * @param id
     * @return
     */
    @GetMapping("/admin/category/{id}")
    @Cacheable(value = "categoryCache", key = "'categoryDetail' + '_' + #id")
    public Result<Category> getById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 更新游戏分类关系
     * @param gameCategoriesDTO
     * @return
     */
    @PutMapping("/admin/categories")
    public Result<String> updateCategoriesInGame(@RequestBody GameCategoriesDTO gameCategoriesDTO) {
        categoryService.updateCategoriesInGame(gameCategoriesDTO);
        return Result.success();
    }

    /**
     * 获取所有分类
     * @param name
     * @return
     */
    @GetMapping("/category")
    public Result<List<CategoryVO>> getAllCategories(@RequestParam(required = false) String name) {
        List<CategoryVO> list = categoryService.getAllCategories(name);
        return Result.success(list);
    }

    /**
     * 为游戏添加分类
     * @param gameCategoriesDTO
     * @return
     */
    @PostMapping("admin/categories")
    public Result<String> addCategoriesInGame(@RequestBody GameCategoriesDTO gameCategoriesDTO) {
        categoryService.addCategoriesInGame(gameCategoriesDTO);
        return Result.success();
    }

    /**
     * koko首页获取分页列表信息
     * @param categoryCardDTO
     * @return
     */
    @GetMapping("/recommend/category")
    public Result<List<CategoryVO>> getCategoryList(CategoryCardDTO categoryCardDTO) {
        List<CategoryVO> list = categoryService.getCategoryList(categoryCardDTO);
        return Result.success(list);
    }

    /**
     * 根据id查询游戏分类信息
     * @param id
     * @return
     */
    @GetMapping("/category/{id}")
    public Result<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }
}
